package org.open.sharedRadar;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;

import android.widget.Toast;


public class AddLocation extends Activity implements LocationListener
{
	LocationManager locManager;
	EditText afisareLongitudine;
	EditText afisareLatitudine;
	Button salveazaAcum;
	Button renunta;
	EditText longText;
	EditText latText;
	EditText observatii;	
	Geocoder geocoder;
	String dateToString = DateFormat.getDateInstance().format(new Date());
	Locatie locatieDeCompletat;
	Locatie locatieCompletata;
	Cursor locatii;
	
	
	String deAfisat;
	
	
	public static ArrayList<Locatie> locatiiLst;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addlocation);
		observatii = (EditText) findViewById(R.id.editObservation);
		afisareLongitudine = (EditText) findViewById(R.id.pozLong);
		afisareLongitudine.setText(FindCar.getLng());
		
		afisareLatitudine = (EditText) findViewById(R.id.pozLat);
		afisareLatitudine.setText(FindCar.getLat());
		
		final DBAdapter myDBAdapter = new DBAdapter(this);
		
		
		salveazaAcum = (Button) findViewById(R.id.btnSave);
		
		salveazaAcum.setOnClickListener(new OnClickListener(){
        	
			public void onClick(View v){				
				locatieCompletata = AddLocation.this.salveazaLocatieNoua();
				myDBAdapter.open();
				myDBAdapter.salveazaLocatieCurenta(locatieCompletata);
				Toast.makeText(getBaseContext(), "Locatie curenta salvata", Toast.LENGTH_SHORT).show();
				/*
				locatii = myDBAdapter.getAllLocatii();
				//locatiiLst = new ArrayList<Locatie>();
				Locatie locatieCurenta;
				locatii.moveToLast();
				//while (locatii.isAfterLast() == false){
				locatieCurenta = new Locatie();
				locatieCurenta.setId(Integer.valueOf(locatii.getInt(0)));
				locatieCurenta.setPozLat(locatii.getString(2));
				locatieCurenta.setObservatie(locatii.getString(3));	
				*/						
				Intent iAdd = new Intent(AddLocation.this, FindCar.class);
        		startActivity(iAdd);

        		myDBAdapter.close();
				
        	}
		});
		renunta = (Button) findViewById(R.id.btnCancel);
		renunta.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {			
				Toast.makeText(getBaseContext(), "Locatia curenta nu a fost salvata!", Toast.LENGTH_SHORT).show();
				Intent iAdd = new Intent(AddLocation.this, FindCar.class);
        		startActivity(iAdd);
				
			}
		});  
	
		
	}
	
	public Locatie salveazaLocatieNoua(){
		locatieDeCompletat = new Locatie();		
		locatieDeCompletat.setPozLong(afisareLongitudine.getText().toString());
		locatieDeCompletat.setPozLat(afisareLatitudine.getText().toString());
		locatieDeCompletat.setObservatie(observatii.getText().toString());
		locatieDeCompletat.setData(dateToString);
		return locatieDeCompletat;
	}
	
	public void onCheckedChanged(CompoundButton arg0, boolean arg1) {}

	public void onLocationChanged(Location arg0) {}

	public void onProviderDisabled(String arg0) {}

	public void onProviderEnabled(String arg0) {}

	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
    
}